package com.apes.fn.mdm.purchase;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.tms.model.Site;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 功能：采购组
 *
 * @author Linwz
 * @create 2021-02-24 9:46
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PurchasingGroup.CONST_TABLE_NAME)
public class PurchasingGroup extends Model {
    public static final String CONST_TABLE_NAME = "cm_purchasing_group";

    @Id
    @Column(name = "id", length = 4)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "采购组",padding = 4)
    @Comment("编码")
    private String id;

    @Name
    @Column(name = "name", length = 30)
    @Comment("名称")
    private String name;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site", referencedColumnName = "id")
    @Comment("站点")
    private Site site;  //站点

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效")
    private boolean valid = true; //有效

    @Column(name = "virtual", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("虚拟")
    private boolean virtual  = false;  //是否需要供应商直供的站点

    @Column(name = "remark", length = 128)
    @Comment("备注")
    private String remark;

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "cm_purchasing_operable",
            joinColumns = {@JoinColumn(name = "purchasing_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "operable_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_purchasing_ope_id", columnList = "purchasing_id")}
    )
    private List<PartyRole> operables = new ArrayList<>();//可操作人员

    // 定义可操作人员前端界面转化
    @Transient
    private List<JSONObject> operableLits;

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "cm_purchasing_dept",
            joinColumns = {@JoinColumn(name = "purchasing_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "dept_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_purchasing_id", columnList = "purchasing_id")}
    )
    private List<Dept> depts = new ArrayList<>();//门店

    // 定义门店前端界面转化
    @Transient
    private List<JSONObject> deptLits;

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "cm_purchasing_supplier",
            joinColumns = {@JoinColumn(name = "purchasing_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "supplier_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_purchasing_sup_id", columnList = "purchasing_id")}
    )
    private List<PartyRole> suppliers = new ArrayList<>();

    // 定义供应商前端界面转化
    @Transient
    private List<JSONObject> supplierLits;

}
